# 获取当前用户的券
import json
import threading
import time

import requests


def printf(s):
    print('[%s][%s]%s' % (threading.current_thread().getName(), time.strftime("%X"), s))


def get_coupon(token, memberid, status):
    headers = {
        'charset': 'utf-8',
        'appid': 'wx07dfb5d79541eca9',
        'referer': 'https://servicewechat.com/wx07dfb5d79541eca9/107/page-frame.html',
        'token': token,
        'content-type': 'application/json',
        'memberid': memberid,
        'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; MP1503 Build/MRA58K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/44.0.2403.119 Mobile Safari/537.36 MicroMessenger/6.7.3.1360(0x2607033A) NetType/WIFI Language/zh_CN Process/appbrand0',
        'Host': 'api.beyonds.com',
    }
    params = (
        ('statuses', status),
        ('page', '1'),
        ('pageSize', '10'),
    )
    response = requests.get('https://api.beyonds.com/wdmp/coupon/v1/getCodeList', headers=headers, params=params)
    printf(response.text)
    result = json.loads(response.text)
    return result


def get_all_coupons(token, memberid):
    unuse_coupon = []
    non_coupon = []
    # 未使用
    unuse = get_coupon(token, memberid, "0")
    unuse_list = unuse["data"]["list"]
    for i in unuse_list:
        unuse_coupon.append(i["code"])
    coupon1 = get_coupon(token, memberid, "1")["data"]["list"]
    for i in coupon1:
        printf(i)
        non_coupon.append(i["code"])
    coupon2 = get_coupon(token, memberid, "2")["data"]["list"]
    for i in coupon2:
        non_coupon.append(i["code"])
    coupon3 = get_coupon(token, memberid, "-1")["data"]["list"]
    for i in coupon3:
        non_coupon.append(i["code"])
    return unuse_coupon


if __name__ == '__main__':
    printf(get_all_coupons("09c217b77c604f4eb1f8cca56d09f962", "17000001001539396"))
